package junit;

import static org.junit.Assert.*;
import businessobjects.User;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class UserTestHibernate {

	static SessionFactory sessionFactory;
	static ServiceRegistry serviceRegistry;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		
		
		Configuration  conf = new Configuration().configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(
				conf.getProperties()).buildServiceRegistry();
		sessionFactory = conf.buildSessionFactory(serviceRegistry);

	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		sessionFactory.close();
	}

	@Test
	public void saveAutoObject() {

		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		User user = new User();
		user.setId(13);
//		user.setNachname("Mustermann");
//		user.setVorname("Max");
		user.setUsername("maximax");
		user.setPasswort("1337");
		session.save(user); // save POJO, mapping can be found in
							// User.hbm.xml
		trans.commit();
		session.close();
		assertTrue(user.getId() > 0);
//		assertEquals("Nachname", "Mustermann", user.getNachname());
//		assertEquals("Vorname", "Max", user.getVorname());
		assertEquals("Username", "maximax", user.getUsername());
		

		
	}
}
